﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Library.Actions;
using Eacademy.Library.Membership;

namespace Eacademy.Areas.Student.Controllers
{
    [ProfileControllerFilter]
    public class MessagesController : Controller
    {
        //
        // GET: /Student/Messages/
        private IMentorRepository _mentorRepository;
        private IMessageRepository _messageRepository;

        public MessagesController(IMentorRepository mentorRepository,IMessageRepository messageRepository)
        {
            _mentorRepository = mentorRepository;
            _messageRepository = messageRepository;
        }
        CustomMembershipProvider provider = (CustomMembershipProvider)Membership.Provider;
        private const int DefaultPageSize = 10;

        public ActionResult Index(int? page)
        {
            
            int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
            int totalcount = 0;
            List<MessageViewModel> messages = _messageRepository.GetConversationsForUserId((int)provider.User.UserId,currentPageIndex+1,DefaultPageSize,out totalcount);

            IPagedList<MessageViewModel> messageList = messages.ToPagedList(currentPageIndex, DefaultPageSize,
                                                                            totalcount);
            return View(messageList);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(StudentMessagesViewModel messagesViewModel)
        {
            messagesViewModel.UserId = (int)provider.User.UserId;
            _messageRepository.CreateMessage(messagesViewModel);
            this.FlashDone("Your message was successfully sent!");
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Reply(StudentMessagesViewModel messagesViewModel)
        {
            messagesViewModel.UserId = (int)provider.User.UserId;
            _messageRepository.ReplyMessage(messagesViewModel);
            this.FlashDone("Your message was successfully sent!");
            return RedirectToAction("Index");
        }



        public ActionResult Details(int id)
        {
            MessageViewModel message = _messageRepository.GetSubjectByConversationId(id);
            ViewData["subject"] = message.Subject;
            List<MessageViewModel> messages =  _messageRepository.GetMessagesForConversationId(id);
            ViewData["cid"] = id;
            return View(messages);
        }

        public ActionResult GetMentors(string q)
        {
            MentorFilterViewModel mentorFilterViewModel = new MentorFilterViewModel();
            mentorFilterViewModel.Name = q;
            var mentors = _mentorRepository.GetMentors(mentorFilterViewModel).Select(mentor => new
            {
                id = mentor.UserId,
                name = String.Format("{0} {1} {2}", mentor.FirstName, mentor.MiddleName, mentor.LastName)
            }).ToArray();

            return Json(mentors, JsonRequestBehavior.AllowGet);
        }
    }
}
